const log4js = require('log4js');

import {ipcRenderer} from "electron";

const getLogger = (category) => {
    return log4js.getLogger(category);
};

// 日志工具对象
export const Logger = {
    trace: (category, message) => {
        getLogger(category).trace(message);
    },
    debug: (category, message) => {
        getLogger(category).debug(message);
    },
    info: (category, message) => {
        getLogger(category).info(message);
    },
    warn: (category, message) => {
        getLogger(category).warn(message);
    },
    error: (category, message) => {
        getLogger(category).error(message);
    },
    fatal: (category, message) => {
        getLogger(category).fatal(message);
    },
    // 由于mark方法与fatal重复，考虑是否需要保留或修改
    mark: (category, message) => {
        getLogger(category).fatal(message); // 或者根据需求更改此方法的行为
    },
};


export const LogUtils = {
    trace:(category, message)=> {
        ipcRenderer.send('log4js:trace', {category, message});
    },
    debug:(category, message)=> {
        ipcRenderer.send('log4js:debug', {category, message});
    },
    info:(category, message)=> {
        ipcRenderer.send('log4js:info', {category, message});
    },
    warn:(category, message)=> {
        ipcRenderer.send('log4js:warn', {category, message});
    },
    error:(category, message)=> {
        ipcRenderer.send('log4js:error', {category, message});
    },
    fatal:(category, message)=> {
        ipcRenderer.send('log4js:fatal', {category, message});
    },
    mark:(category, message)=> {
        ipcRenderer.send('log4js:mark', {category, message});
    },

 }

// 确保程序退出时关闭所有appenders
process.on('exit', () => {
    log4js.shutdown(() => {
        console.log('所有的日志appenders已关闭。');
    });
});

